﻿using System.Linq;
using TaleWorlds.CampaignSystem;

namespace MarriageFertility.Actions;

public static class DivorceAction
{
    public static void Apply(Hero hero1, Hero hero2)
    {
        if (hero1 == null || hero2 == null) return;
        hero1.Spouse = null;
        hero2.Spouse = null;
        hero1.ExSpouses?.Remove(hero2);
        hero2.ExSpouses?.Remove(hero1);
        if (hero1.ExSpouses?.Count(s => s.IsAlive) > 0)
        {
            Hero newSpouse = hero1.ExSpouses.First(s => s.IsAlive);
            hero1.Spouse = newSpouse;
            hero1.ExSpouses.Remove(newSpouse);
        }
    }
}